package com.mcxx.modules.base.service.read;

import com.mcxx.modules.base.dao.read.AreaGroupReadDao;
import com.mcxx.modules.base.dao.read.AreaMappingReadDao;
import com.mcxx.modules.base.dao.read.TAAreaReadDao;
import com.mcxx.modules.base.entity.AdmindivisionEntity;
import com.mcxx.modules.base.entity.AreaEntity;
import com.mcxx.modules.base.entity.AreaGroupEntity;
import com.mcxx.modules.base.entity.AreaMappingEntity;
import com.mcxx.modules.dictionary.dto.AreaNewDTO;
import com.mcxx.modules.system.dto.AreaMappingDTO;
import com.mcxx.modules.system.param.AreaMappingParam;
import com.mcxx.result.BaseResult;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.component.org.orguser.area.service.read.AreaManagementReadService;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.core.utils.TreeUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("areaReadService")
@TaTransactional(readOnly = true)
public class AreaReadService {

    private final static Logger logger = LoggerFactory.getLogger(AreaReadService.class);
    @Autowired
    private AreaManagementReadService areaManagementReadService;
    @Autowired
    private AreaMappingReadDao areaMappingReadDao;
    @Autowired
    private AreaGroupReadDao areaGroupReadDao;
    @Autowired
    private TAAreaReadDao taAreaReadDao;

    public TaAreaPo getAreaByCode(String code) {
        TaAreaPo taAreaPo = areaManagementReadService.queryAreaByAreaCode(code);
        return taAreaPo;
    }
    public int getCountByAreaCode(String userAreaCode,String areaCode) {
        return  taAreaReadDao.getCountByAreaCode(userAreaCode,areaCode);
    }

    public List<TaAreaPo> getChildrenByLevel(String areaCode, List<Integer> levels) {
        return taAreaReadDao.getChildrenByLevel(areaCode, levels);
    }

    public List<Map<String, Object>> getAllArea(String areaId, String effective) {
        return areaManagementReadService.queryArea(areaId, effective);
    }

    public BaseResult<List<AreaNewDTO>> listGroupByAreaCode(String rescueCode) {
        BaseResult<List<AreaNewDTO>> resultMessage = new BaseResult<>();
        List<String> financeidList = areaMappingReadDao.getFinanceIdByRescueCode(rescueCode);
        if (CollectionUtils.isEmpty(financeidList)) {
//            resultMessage.setCode(FailureCode.ERR_9999.getCode());


            List<AreaNewDTO> list=new ArrayList<AreaNewDTO>();
            AreaNewDTO dto=new AreaNewDTO();
            dto.setValue("111111111111");
            dto.setName("预备组");
            list.add(dto);
            resultMessage.setData(list);
            return resultMessage;
        }
        try {
            List<AdmindivisionEntity> areaList = areaGroupReadDao.queryAreaGroupByFinanceId(financeidList);
            if (CollectionUtils.isEmpty(areaList)) {
                List<AreaNewDTO> list=new ArrayList<AreaNewDTO>();
                AreaNewDTO dto=new AreaNewDTO();
                dto.setValue("111111111111");
                dto.setName("预备组");
                list.add(dto);
                resultMessage.setData(list);
                return resultMessage;
            }
            List<AreaNewDTO> list=new ArrayList<AreaNewDTO>();
            for(AdmindivisionEntity e : areaList){
                AreaNewDTO dto=new AreaNewDTO();
                dto.setValue(e.getCode());
                dto.setName(e.getName());


                list.add(dto);
            }
            resultMessage.setData(list);
            return resultMessage;
        } catch (Exception e) {
            logger.error("根据地区码查询组代码信息" + e.getMessage());
            e.printStackTrace();
            resultMessage.setCode(FailureCode.ERR_9999.getCode());
            resultMessage.setMsg(FailureCode.ERR_9999.getMessage());
            return resultMessage;
        }
    }

    private List<AreaNewDTO> getChildrenList(String id){
        List<AdmindivisionEntity> sonList = areaGroupReadDao.queryChildrenAreaGroupByPid(id);
        List<AreaNewDTO> list1=new ArrayList<AreaNewDTO>();
        for(AdmindivisionEntity e1:sonList){
            AreaNewDTO dto1=new AreaNewDTO();
            dto1.setValue(e1.getCode());
            dto1.setName(e1.getName());
            List<AreaNewDTO> list2=getChildrenList(e1.getId());
            dto1.setChildren(list2);
            list1.add(dto1);
        }
        return list1;
    }

    public String getTenantIdRela(String rescuecode){
        return   areaMappingReadDao.getFinancecodeByRescueCode(rescuecode);
    }


    public String getRescueCodeByFinancecode(String financecode){
        return   areaMappingReadDao.getRescueCodeByFinancecode(financecode);
    }


    public int  getCountByFinanceId(String id){
        return   areaMappingReadDao.getCountByFinanceId(id);
    }


    public List<AreaEntity>  getInfoByName(String financeName){
        return   taAreaReadDao.getInfoByName(financeName);
    }


    public List<AreaGroupEntity>  getAreaInfoByLevel(String level, String financeCountyCode){
        return   areaGroupReadDao.getAreaInfoByLevel(level,financeCountyCode);
    }


    public List<String>  getRescueCodeByFinanceId(String financeId){
        return   areaMappingReadDao.getRescueCodeByFinanceId(financeId);
    }

    public List<AreaEntity>  getInfoByNameAndPid(String financeName,String pId,String level){
        return   taAreaReadDao.getInfoByNameAndPid(financeName,pId,level);
    }


    public int  getNumById(String id){
        return   areaGroupReadDao.getNumById(id);
    }






//        param.setAreaLevel(accountVo.getAreaLevel());








    public List<Map<String, Object>> queryAreaMappingList(AreaMappingParam param, UserAccountVo accountVo) {

        param.setAreaLevel(accountVo.getAreaLevel());
        if(StringUtils.isEmpty(param.getAreaCode())){
            param.setAreaCode(accountVo.getAreaCode());
        }
        param.setLoginUserLevel(accountVo.getAreaLevel());
        List<AreaMappingDTO> AreaMappingList = areaMappingReadDao.queryAreaMappingList(param);

        List<Map<String, Object>> ss = new ArrayList<>();
        if(null != AreaMappingList && !AreaMappingList.isEmpty()){
            for(AreaMappingDTO aa :AreaMappingList){
                Map<String, Object> map = new HashMap(32);
                map.put("id",aa.getId());
                map.put("parentId",aa.getParentId());
                map.put("areaLevel",aa.getAreaLevel());
                map.put("loginUserLevel",aa.getLoginUserLevel());
                map.put("areaCode",aa.getAreaCode());
                map.put("areaName",aa.getAreaName());
                map.put("financeName",aa.getFinanceName());
                map.put("financeCode",aa.getFinanceCode());
                map.put("childNum",aa.getChildNum());

                if (aa.getChildNum() != null && aa.getChildNum() > 0) {
                    map.put("children", new ArrayList());
                }

                ss.add(map);
            }
        }


        return TreeUtil.generateTreeList(ss, "id", "parentId");

    }

    public List<AreaMappingDTO> downLoadAreaMappingList(AreaMappingParam param, UserAccountVo accountVo) {

        param.setAreaLevel(accountVo.getAreaLevel());
        if (StringUtils.isEmpty(param.getAreaCode())) {
            param.setAreaCode(accountVo.getAreaCode());
        }
        param.setLoginUserLevel(accountVo.getAreaLevel());
        return  areaMappingReadDao.queryAreaMappingList(param);

    }


    public List<AreaMappingDTO> queryAreaMappingInfo(AreaMappingParam param,UserAccountVo accountVo) {


        List<AreaMappingDTO> list = areaMappingReadDao.queryAreaMappingInfo(param);


        return list;
    }


    public List<AreaMappingDTO> getFinanceInfo(AreaMappingParam param,UserAccountVo accountVo) {


        if(Constant.UserLevel.COUNTY.equals(param.getAreaLevel())){
            List<AreaMappingDTO> list = areaMappingReadDao.getFinanceCountyInfo(param);
            return list;
        }

        List<AreaMappingDTO> list = areaMappingReadDao.getFinanceInfo(param);


        return list;
    }


    public AreaGroupEntity getAreaGroupByFinanceCode(String financecode){
        return   areaGroupReadDao.getAreaGroupByFinanceCode(financecode);
    }


    public List<AreaGroupEntity> getAreaGroupByPid(String pId){
        return   areaGroupReadDao.getAreaGroupByPid(pId);
    }


    public List<AreaEntity>  getInfoByPid(String pId){
        return   taAreaReadDao.getInfoByPid(pId);
    }


    public List<AreaMappingEntity>  getAreaMappingInfo(String areaCode){
        return   areaMappingReadDao.getAreaMappingInfo(areaCode);
    }


    public List<AreaMappingEntity>  getAreaMappingInfoByFinanceCode(String tenantId){
        return   areaMappingReadDao.getAreaMappingInfoByFinanceCode(tenantId);
    }

    public List<AreaGroupEntity> getAgentBanks(UserAccountVo accountVo) {
        String areaCode = null;

        if(Constant.UserLevel.COUNTY.equals(accountVo.getAreaLevel())){
            areaCode = accountVo.getAreaCode().substring(0,6);
        }else if(Constant.UserLevel.STREET.equals(accountVo.getAreaLevel())){
            areaCode = accountVo.getAreaCode().substring(0,9)+"000";
        }
        List<AreaGroupEntity> areaGroupList = areaGroupReadDao.getAgentBanks(areaCode);
        return areaGroupList;
    }


    public List<AreaMappingDTO>  getAreaCodeBycCodeALevle(String areaCode){
        return  areaMappingReadDao.getAreaCodeBycCodeALevle(areaCode);
    }

}
